import Vue from 'vue'
import Router from 'vue-router'
Vue.use(Router)

const VueRouter = [
  {
    path: '/',
    redirect: '/index'
  },
  {
    path: '/index',
    component: (resolve) => require(['@/views/home/index'], resolve),
    name: 'home',
    meta: {
      title: '首页',
      keepAlive: true,
      tabBar: true
    }
  },
  {
    path: '/select-account',
    component: (resolve) => require(['@/views/home/select-account'], resolve),
    name: 'selectAccount',
    meta: {
      title: '选择账号',
      keepAlive: false,
      tabBar: false
    }
  },
  {
    path: '/me',
    component: (resolve) => require(['@/views/me/index'], resolve),
    name: 'me',
    meta: {
      title: '我的',
      keepAlive: false,
      tabBar: true
    }
  },
  {
    path: '/scan-details',
    component: (resolve) => require(['@/views/scan/scan-details'], resolve),
    name: 'scan-details',
    meta: {
      title: '商户详情',
      keepAlive: false
    }
  },
  {
    path: '/immediate-sampling',
    component: (resolve) =>
      require(['@/views/sampling-ledger/immediate-sampling.vue'], resolve),
    name: 'immediateSampling',
    meta: {
      title: '立即采样',
      keepAlive: true
    }
  },
  {
    path: '/add-supplyInfo',
    component: (resolve) =>
      require(['@/views/sampling-ledger/add-supplyInfo.vue'], resolve),
    name: 'addSupplyInfo',
    meta: {
      title: '添加供货信息',
      keepAlive: true
    }
  },
  {
    path: '/inspection-subject',
    component: (resolve) => require(['@/views/inspection-subject'], resolve),
    name: 'inspectionSubject',
    meta: {
      title: '快检主体',
      keepAlive: false
    }
  },
  {
    path: '/subject-details',
    component: (resolve) =>
      require(['@/views/inspection-subject/subject-details'], resolve),
    name: 'subjectDetails',
    meta: {
      title: '快检主体详情',
      keepAlive: false
    }
  },
  {
    path: '/new-inspectSubject',
    component: (resolve) =>
      require(['@/views/inspection-subject/new-inspectSubject'], resolve),
    name: 'newInspectSubject',
    meta: {
      title: '新增快检主体',
      keepAlive: true
    }
  },
  {
    path: '/subject-list',
    component: (resolve) =>
      require(['@/views/inspection-subject/subject-list'], resolve),
    name: 'subjectList',
    meta: {
      title: '主体列表',
      keepAlive: false
    }
  },
  {
    path: '/select-purchaseList',
    component: (resolve) =>
      require(['@/views/inspection-subject/select-purchaseList'], resolve),
    name: 'selectPurchaseList',
    meta: {
      title: '选择进货单',
      keepAlive: false
    }
  },
  {
    path: '/new-purchaseList',
    component: (resolve) =>
      require(['@/views/inspection-subject/new-purchaseList'], resolve),
    name: 'newPurchaseList',
    meta: {
      title: '新增进货单',
      keepAlive: false
    }
  },
  {
    path: '/select-sampler',
    component: (resolve) =>
      require(['@/views/inspection-subject/select-sampler'], resolve),
    name: 'selectSampler',
    meta: {
      title: '选择采样人员',
      keepAlive: false
    }
  },
  {
    path: '/select-sample',
    component: (resolve) =>
      require(['@/views/inspection-subject/select-sample'], resolve),
    name: 'selectSample',
    meta: {
      title: '选择样品',
      keepAlive: false
    }
  },
  {
    path: '/select-market',
    component: (resolve) =>
      require(['@/views/inspection-subject/select-market'], resolve),
    name: 'selectMarket',
    meta: {
      title: '选择所属市场',
      keepAlive: false
    }
  },
  {
    path: '/sampling-ledger',
    component: (resolve) => require(['@/views/sampling-ledger'], resolve),
    name: 'samplingLedger',
    meta: {
      title: '采样台账',
      keepAlive: true
    }
  },
  {
    path: '/rerecord-purchaseInfo',
    component: (resolve) =>
      require(['@/views/sampling-ledger/rerecord-purchaseInfo'], resolve),
    name: 'rerecordPurchaseInfo',
    meta: {
      title: '补充进货信息',
      keepAlive: false
    }
  },
  {
    path: '/sampling-search',
    component: (resolve) =>
      require(['@/views/sampling-ledger/sampling-search'], resolve),
    name: 'samplingSearch',
    meta: {
      title: '搜索页',
      keepAlive: false
    }
  },
  {
    path: '/sampling-details',
    component: (resolve) =>
      require(['@/views/sampling-ledger/sampling-details'], resolve),
    name: 'samplingDetails',
    meta: {
      title: '采样详情',
      keepAlive: false
    }
  },
  {
    path: '/edit-draft',
    component: (resolve) =>
      require(['@/views/sampling-ledger/edit-draft'], resolve),
    name: 'editDraft',
    meta: {
      title: '编辑草稿',
      keepAlive: true
    }
  },
  {
    path: '/person-info',
    component: (resolve) => require(['@/views/me/person-info'], resolve),
    name: 'personInfo',
    meta: {
      title: '从业人员列表页',
      keepAlive: false
    }
  },
  {
    path: '/equipment-info',
    component: (resolve) => require(['@/views/me/equipment-info'], resolve),
    name: 'equipmentInfo',
    meta: {
      title: '快速检测设备管理信息',
      keepAlive: false
    }
  },
  {
    path: '/add-person',
    component: (resolve) => require(['@/views/me/add-person'], resolve),
    name: 'addPerson',
    meta: {
      title: '从业人员新增页',
      keepAlive: false
    }
  },
  {
    path: '/add-equipment',
    component: (resolve) => require(['@/views/me/add-equipment'], resolve),
    name: 'addEquipment',
    meta: {
      title: '快速检测设备管理信息',
      keepAlive: false
    }
  },
  {
    path: '/manage-person',
    component: (resolve) => require(['@/views/me/manage-person'], resolve),
    name: 'managePerson',
    meta: {
      title: '从业人员管理页',
      keepAlive: false
    }
  },
  {
    path: '/manage-equipment',
    component: (resolve) => require(['@/views/me/manage-equipment'], resolve),
    name: 'manageEquipment',
    meta: {
      title: '管理检测设备信息',
      keepAlive: false
    }
  },
  {
    path: '/person-details',
    component: (resolve) => require(['@/views/me/person-details'], resolve),
    name: 'personDetails',
    meta: {
      title: '从业人员详情页',
      keepAlive: false
    }
  },
  {
    path: '/equipment-details',
    component: (resolve) => require(['@/views/me/equipment-details'], resolve),
    name: 'equipmentDetails',
    meta: {
      title: '检测设备详情',
      keepAlive: false
    }
  },
  {
    path: '/submit-success',
    component: (resolve) =>
      require(['@/views/sampling-ledger/submit-success'], resolve),
    name: 'submitSuccess',
    meta: {
      title: '立即采样',
      keepAlive: false
    }
  },
  {
    path: '/select-address',
    component: (resolve) => require(['@/views/common/select-address'], resolve),
    name: 'selectAddress',
    meta: {
      title: '选择区划',
      keepAlive: false
    }
  },
  {
    path: '/sampling-test',
    component: (resolve) => require(['@/views/sampling-test'], resolve),
    name: 'samplingTest',
    meta: {
      title: '样品检测',
      keepAlive: false
    }
  },
  {
    path: '/immediate-detection',
    component: (resolve) =>
      require(['@/views/sampling-test/immediate-detection'], resolve),
    name: 'immediateDetection',
    meta: {
      title: '立即检测',
      keepAlive: true
    }
  },
  {
    path: '/sampling-test/select-person',
    component: (resolve) =>
      require(['@/views/sampling-test/select-person'], resolve),
    name: 'selectPersonOfDetection',
    meta: {
      title: '选择检测人员',
      keepAlive: false
    }
  },
  {
    path: '/sampling-test/select-product',
    component: (resolve) =>
      require(['@/views/sampling-test/select-product'], resolve),
    name: 'selectProductOfDetection',
    meta: {
      title: '选择快检产品',
      keepAlive: false
    }
  },
  {
    path: '/sampling-test/select-project',
    component: (resolve) =>
      require(['@/views/sampling-test/select-project'], resolve),
    name: 'selectProjectOfDetection',
    meta: {
      title: '选择检测项目',
      keepAlive: false
    }
  },
  {
    path: '/sampling-test/select-equipment',
    component: (resolve) =>
      require(['@/views/sampling-test/select-equipment'], resolve),
    name: 'selectEquipmentOfDetection',
    meta: {
      title: '选择设备',
      keepAlive: false
    }
  },
  {
    path: '/sampling-test/product-search',
    component: (resolve) =>
      require(['@/views/sampling-test/product-search'], resolve),
    name: 'productSearch',
    meta: {
      title: '搜索页',
      keepAlive: false
    }
  },
  {
    path: '/inspection-ledger',
    component: (resolve) => require(['@/views/inspection-ledger'], resolve),
    name: 'inspectionLedger',
    meta: {
      title: '检测台账',
      keepAlive: true
    }
  },
  {
    path: '/inspection-search',
    component: (resolve) =>
      require(['@/views//inspection-ledger/inspection-search'], resolve),
    name: 'inspectionSearch',
    meta: {
      title: '搜索页',
      keepAlive: false
    }
  },
  {
    path: '/inspection-details',
    component: (resolve) =>
      require(['@/views//inspection-ledger/inspection-details'], resolve),
    name: 'inspectionDetails',
    meta: {
      title: '检测详情',
      keepAlive: false
    }
  },
  {
    path: '/sign',
    component: (resolve) =>
      require(['@/views/common/sign'], resolve),
    name: 'VueTeaSign',
    meta: {
      title: '签名',
      keepAlive: false
    }
  },
  {
    path: '*',
    component: (resolve) => require(['@/views/common/404'], resolve),
    name: 'submitSuccess',
    meta: {
      title: '立即采样',
      keepAlive: false
    }
  }
]

const router = new Router({
  mode: 'hash',
  routes: VueRouter,

  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { x: 0, y: 0 }
    }
  }
})

// 注意：刚打开页面加载的第一个路由并不会进入beforeEach里
router.beforeEach((to, from, next) => {
  // console.log('to===>', to)
  // console.log('from===>', from)
  document.title = to.query.routeTitle
    ? to.query.routeTitle
    : `${to.meta.title}`
  next()
})

// 全局后置钩子：可以捕捉到刚打开页面加载的第一个路由
router.afterEach((to) => {
  // console.log('afterEach',to);
  /* 路由发生变化修改页面title */
  document.title = to.query.routeTitle
    ? to.query.routeTitle
    : `${to.meta.title}`
})

// 解决导航重复问题
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

export default router
